<template>
  <div class="app-container">
    <el-row :gutter="20" class="header">
      <el-col :span="15">
        <el-form-item label="党员">
          <el-select v-model="queryForm.nickname" class="widthForm" placeholder="请选择党员">
            <el-option v-for="item in selectUserList" :key="item.id" :label="item.nickname" :value="item.nickname" />
          </el-select>
        </el-form-item>

        <el-form-item label="党支部">
          <el-select v-model="queryForm.branchName" class="widthForm" placeholder="请选择党支部">
            <el-option v-for="item in SelectBranchList" :key="item.id" :label="item.branchName"
              :value="item.branchName" />
          </el-select>
        </el-form-item>

        <el-form-item label="二级党委">
          <el-select v-model="queryForm.commiteName" class="widthForm" placeholder="请选择党支部">
            <el-option v-for="item in SelectCommiteList" :key="item.id" :label="item.commiteName"
              :value="item.commiteName" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-button type="info" :icon="Document" @click="resetSearch">重置搜索</el-button>
      <el-button type="primary" :icon="Search" @click="initUserList" v-if="hasAuth('cms:menu:query')">搜索</el-button>
      <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue()"
        v-if="hasAuth('cms:party:add')">新增</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="nickname" label="党员姓名" align="center" />
      <el-table-column prop="sex" label="性别" width="50" align="center" />
      <el-table-column prop="commiteName" label="二级党委名称" align="center" />
      <el-table-column prop="branchName" label="党支部名称" align="center" />
      <el-table-column prop="joinDate" label="入党时间" align="center" />
      <el-table-column prop="email" label="邮箱" width="200" align="center" />

      <el-table-column prop="action" label="操作" fixed="right" align="center">
        <template v-slot="scope">
          <el-button v-if="scope.row.username != 'admin' && hasAuth('cms:party:edit')" type="primary" :icon="Edit"
            @click="handleDialogValue(scope.row.id)" />

          <el-popconfirm v-if="scope.row.username != 'admin'" title="您确定要开除党籍嘛？"
            @confirm="handleDelete(scope.row.id, scope.row.userId)">
            <template #reference>
              <el-button type="danger" :icon="Delete" v-if="hasAuth('cms:party:delete')" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-model:currentPage="queryForm.pageNum" v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 40, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle"
    @initUserList="initUserList"></Dialog>
</template>

<script setup>
import { Search, Delete, DocumentAdd, Edit, Tools, RefreshRight } from "@element-plus/icons-vue";
import { ref } from "vue";
import requestUtil, { getServerUrl } from "@/utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
import Dialog from "./components/dialog";

const queryForm = ref({
  nickname: "",
  commiteName: "",
  branchName: "",
  pageNum: 1,
  pageSize: 10,
});

const total = ref(0);

const tableData = ref([]);

const id = ref(-1);

const sysRoleList = ref([]);

const imageDialogValue = ref({});

const dialogVisible = ref(false);

const roleDialogVisible = ref(false);

const dialogTitle = ref("");

const multipleSelection = ref([]);

const delBtnStatus = ref(true);

const handleSelectionChange = (selection) => {
  multipleSelection.value = selection;
  delBtnStatus.value = selection.length == 0;
};

const initUserList = async () => {
  const res = await requestUtil.post("party/partyList", queryForm.value);

  // 筛选出党员
  tableData.value = res.data.partyList.records;
  total.value = res.data.partyList.total;
};

initUserList();

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1;
  queryForm.value.pageSize = pageSize;
  initUserList();
};

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum;
  initUserList();
};

// 党支部下拉框
const SelectBranchList = ref([]);
const SelectBranchListFun = async () => {
  const res = await requestUtil.get("branch/getBranchSelectList");
  SelectBranchList.value = res.data.branchList;
};

// 二级党委下拉框
const SelectCommiteList = ref([]);
const SelectCommiteListFun = async () => {
  const res = await requestUtil.get("commite/getSelectCommiteList");
  SelectCommiteList.value = res.data.commiteList;
};

// 用户信息下拉框
const selectUserList = ref([]);
const SelectUserListFun = async () => {
  let data = {
    pageNum: 1,
    pageSize: 100,
  };
  const res = await requestUtil.post("sys/user/list", data);
  selectUserList.value = res.data.userList.records;
};
SelectBranchListFun();
SelectCommiteListFun();
SelectUserListFun();

const resetSearch = () => {
  queryForm.value = {};
  initUserList();
};

const handleDialogValue = (userId) => {
  if (userId) {
    id.value = userId;
    dialogTitle.value = "党员信息修改";
  } else {
    id.value = -1;
    dialogTitle.value = "党员添加";
  }
  dialogVisible.value = true;
};

const handleDelete = async (id, userId) => {
  let ids = id + "," + userId;
  let data = { ids };
  const res = await requestUtil.deleteReq(`party/deletePartyById`, data);
  if (res.data.code == 200) {
    ElMessage({
      type: "success",
      message: "开除成功!",
    });
    initUserList();
  } else {
    ElMessage({
      type: "error",
      message: res.data.msg,
    });
  }
};
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;

  .el-col {
    display: flex;
  }

  .el-form-item {
    margin-right: 10px;
  }
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;
}

.el-tag--small {
  margin-left: 5px;
}
</style>
